using System;
using System.Collections.Generic;
using System.Text;
using CreamX.Framework.Game.Components;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;


namespace CreamX.Framework.Game
{
    public class CreamXTileGrid : CreamXObject
    {
        
        public CreamXTileGridComponent _comp;
        public CreamXTileGrid()
        {
            _comp = new CreamXTileGridComponent();
            
            AddComponent(_comp);
        }
        internal override void Update(float gameTime)
        {
            base.Update(gameTime);
        }
        public override void OnRegister()
        {
            Template = false;
            base.OnRegister();
            _comp.Enabled = true;
            _comp = GetComponent<CreamXTileGridComponent>();
            
        }
        internal void ReSetGrid()
        {
            _comp= GetComponent<CreamXTileGridComponent>();
        }
        public override void Draw(SpriteBatch spritebatch)
        {
            
            foreach (TileLayer layer in _comp.TileLayers)
            {
                if (!layer.Visible)
                    continue;

                //base.Draw(spritebatch);
                if (layer.Tiles != null)
                {
                    foreach (Tile tile in layer.Tiles)
                    {
                        if (tile.TileNumber > -1)
                        {
                            Vector2 orig = new Vector2(tile.SourceRectangle.Width / 2, tile.SourceRectangle.Height / 2);
                            Vector2 pos = Position + tile.Position + orig;

                            if (!IgnoreCameraPosition)
                                pos = Scene.Camera.GetRenderPosition(pos);
                            else
                                pos *= Scene.Camera.Zoom;



                            Rectangle _rect = new Rectangle(
                                (int)(pos.X),
                                (int)(pos.Y), 
                                (int)(_comp.TileDisplaySizeX * Scene.Camera.Zoom), 
                                (int)(_comp.TileDisplaySizeY * Scene.Camera.Zoom));
                             _rect.Offset(-1, -1);
                            _rect.Inflate(2, 2);
                            spritebatch.Draw(Material.Texture,
                                _rect,
                                tile.SourceRectangle, Color, tile.Rotation, orig, SpriteEffects.None, Layer / 10f);
                        }
                    }
                }
            }
            //if (DebugRender)
            //{
            //    foreach (Tile tile in _comp._tiles)
            //    {
            //        Vector2 __tl = Vector2.Add(tile.Position, Vector2.One);
            //        Vector2 __br = Vector2.Subtract(tile.Position + new Vector2(_comp.TileCutSizeX, _comp.TileCutSizeY), Vector2.One * 2f);
            //        _batch.DrawBox(Scene.Camera.GetRenderPosition(__tl),Scene.Camera.GetRenderPosition(__br), Color.White);
            //    }
            //}
        }
        public bool DebugRender = true;

        new public float DisplayWidth
        {
            get { return _comp.TileCountX * _comp.TileCutSizeX; }
        }
        new public float DisplayHeight
        {
            get { return _comp.TileCountY * _comp.TileCutSizeY; }
        }
    }
}
